package tool

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	//"gorm.io/driver/mysql"
	//"gorm.io/gorm"
	"manhe/model"
)

var DB *gorm.DB

//type Gorm struct {
//	*gorm.DB
//}

//func OpenDB() (*Gorm, error) {
//	data := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", CONFIG.Db.Username, CONFIG.Db.Password, CONFIG.Db.Ip, CONFIG.Db.Host, CONFIG.Db.Dbname)
//	fmt.Println(data)
//	db, err := gorm.Open(mysql.Open(data), &gorm.Config{})
//	if err != nil {
//		return nil, err
//	}
//	//db.LogMode(true)
//	db.AutoMigrate(&model.Goods{}, &model.UserModel{}, &model.BindBox{}, &model.BoxPc{}, &model.BatchPrize{})
//
//	gorm := new(Gorm)
//	gorm.DB = db
//
//	DB = gorm
//	fmt.Println("DB启动成功")
//	return gorm, nil
//}

func OpenDB() (*gorm.DB, error) {
	data := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", CONFIG.Db.Username, CONFIG.Db.Password, CONFIG.Db.Ip, CONFIG.Db.Host, CONFIG.Db.Dbname)
	fmt.Println(data)
	db, err := gorm.Open(CONFIG.Db.Dialect, data)
	if err != nil {
		return nil, err
	}
	db.LogMode(true)
	db.AutoMigrate(&model.Goods{}, &model.UserModel{}, &model.BindBox{}, &model.BoxPc{}, &model.BatchPrize{})

	//gorm := new(Gorm)
	//gorm.DB = db

	DB = db
	fmt.Println("DB启动成功")
	return db, nil
}

func init() {
	OpenDB()
}
